package com.qf.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.PageDto;
import com.qf.product.vo.PageVo;
import com.qf.product.entity.Product;
import com.qf.product.mapper.ProductMapper;
import com.qf.product.service.ProductService;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-10-26
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    public PageVo<Product> page(PageDto<Product> pageDto){
        final Page<Product> objectPage = new Page<>(pageDto.getNow(), pageDto.getSize());
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            queryWrapper.lambda().like(Product::getName,pageDto.getWhere().getName());
        }
        IPage<Product> productIPage=this.baseMapper.selectPage(objectPage,queryWrapper);
        PageVo<Product> pageResult=new PageVo<>();
        pageResult.setTotal(productIPage.getTotal());
        pageResult.setPages(productIPage.getPages());
        pageResult.setList(productIPage.getRecords());
        return pageResult;
    }

    @Override
    public Result<Product> findOneByDeviceId(Long id) {
        if(StringUtils.isEmpty(id)){
            return new Result<>(1,"非法参数");
        }
        Product product = productMapper.findOneByDeviceId(id);
        if (StringUtils.isEmpty(product)){
            return new Result<>(1,"fail");
        }
        return new Result<>(0,"success",product);
    }
}
